package com.czl.module_service.fragment.assetUse

import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.LogUtils
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.base.data.bean.tengyun.SortBean
import com.czl.base.event.LiveBusCenter
import com.czl.module_service.BR
import com.czl.module_service.R
import com.czl.module_service.adapter.asset.UsageSortChoosedAdapter
import com.czl.module_service.databinding.FragmentUsageApplyAddBinding
import com.czl.module_service.databinding.FragmentUsageApplyAddFooterBinding
import com.czl.module_service.databinding.FragmentUsageApplyAddHeaderBinding
import com.czl.module_service.viewmodel.assetUse.UsageApplyAddViewModel

@Route(path = AppConstants.Router.Service.F_SERVICE_USAGE_APPLY_ADD)
class UsageApplyAddFragment : BaseFragment<FragmentUsageApplyAddBinding, UsageApplyAddViewModel>() {

    val list : MutableList<SortBean> = arrayListOf()
    private val footerBinding: FragmentUsageApplyAddFooterBinding by lazy  {
        FragmentUsageApplyAddFooterBinding.inflate(layoutInflater).apply {

        }
    }
    override fun initContentView(): Int {
        return R.layout.fragment_usage_apply_add
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun useBaseLayout(): Boolean {
        return false
    }

    private val mAdapterUsage: UsageSortChoosedAdapter by lazy {
        UsageSortChoosedAdapter(this).apply {
            val headerBinding = FragmentUsageApplyAddHeaderBinding.inflate(layoutInflater)
            headerBinding.setVariable(initVariableId(), viewModel)
            addHeaderView(headerBinding.root)
            footerBinding.setVariable(initVariableId(), viewModel)
            addFooterView(footerBinding.root)
        }
    }

    override fun initData() {
        super.initData()
        initAdapter()
        viewModel.getSortList()
        viewModel.getRoleProcessList()
        viewModel.adapter = mAdapterUsage
    }

    private fun initAdapter() {
        binding.recyclerView.run {
            adapter = mAdapterUsage
        }
    }

    override fun initViewObservable() {
        LiveBusCenter.observeAddChoosedSortEvent(this) {
            LogUtils.e("----->" + it.sortBean)
            if (it.sortBean != null) {
                // 选中的物品添加
                val index = mAdapterUsage.position - 1
                list[index] = it.sortBean!!
                viewModel.list = list
                mAdapterUsage.setList(list)
            }
        }
        LiveBusCenter.observeAddAssetPurchaseSortEvent(this, { itm ->
            if (itm.data != null && itm.data!!.isNotEmpty()) {
                // 选中的物品添加
                list.addAll(itm.data!!)
                viewModel.list = list
                if (list.size > 1) {
                    mAdapterUsage.isDel = true
                }
                var list1 : MutableList<SortBean> = arrayListOf()
                list1 = list.filter {
                    it.sortId != null
                } as MutableList<SortBean>
                mAdapterUsage.setList(list1)
            }
        })
        viewModel.uc.loadCompleteEvent.observe(this, { data ->
            list.add(data[0])
            viewModel.list = list
            LogUtils.e("----->" + list.size)
            if (list.size > 1) {
                mAdapterUsage.isDel = true
            }
            mAdapterUsage.setList(list)
        })
        viewModel.uc.roleCompleteEvent.observe(this, { data ->
            if (data!=null) {
                viewModel.businessType = 0
                footerBinding.btnCommit.text = "提交审批"
            } else {
                viewModel.businessType = 1
                footerBinding.btnCommit.text = "提交"
            }
        })
        viewModel.uc.clearFacilityListEvent.observe(this, {
            viewModel.list.clear()
            viewModel.note.set("")
            val list1 : MutableList<SortBean> = arrayListOf()
            list1.add(SortBean())
            mAdapterUsage.setList(list1)
        })
    }

    fun deleteList(sortBean: SortBean) {
        list.removeAt(sortBean.position!! - 1)
        if (list.size == 1) {
            mAdapterUsage.isDel = false
        }
        mAdapterUsage.setList(list)
    }

}